package net.zjitc.doubao_community_backed.controller;/**
 * @Author 董熠涛
 * @Date 2025/6/19  4:02
 */

/**
 * @Author:董熠涛
 * @ 2025/6/194:02
 */

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import net.zjitc.doubao_community_backed.api.ApiResult;
import net.zjitc.doubao_community_backed.model.dto.LoginDTO;
import net.zjitc.doubao_community_backed.model.dto.RegisterDTO;
import net.zjitc.doubao_community_backed.model.entity.UmsUser;
import net.zjitc.doubao_community_backed.service.IUmsUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

import static net.zjitc.doubao_community_backed.utils.JwtUtil.USER_NAME;

@RestController
@RequestMapping("/ums/user")
public class UmsUserController  {
    @Resource
    private IUmsUserService iUmsUserService;

    @PostMapping("/register")
    public ApiResult<Map<String, Object>> register(@Valid @RequestBody RegisterDTO dto) {
        UmsUser user = iUmsUserService.executeRegister(dto);
        if (ObjectUtils.isEmpty(user)) {
            return ApiResult.failed("账号注册失败");
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("user", user);
        return ApiResult.success(map);
    }

    @PostMapping("/login")
    public ApiResult<Map<String, String>> login(@Valid @RequestBody LoginDTO dto) {
        String token = iUmsUserService.executeLogin(dto);
        if (ObjectUtils.isEmpty(token)) {
            return ApiResult.failed("账号密码错误");
        }
        Map<String, String> map = new HashMap<>(16);
        map.put("token", token);
        return ApiResult.success(map, "登录成功");
    }

    @GetMapping("/info")
    public ApiResult<UmsUser> getUser(@RequestHeader(value = USER_NAME) String userName) {
        UmsUser user = iUmsUserService.getUserByUsername(userName);
        return ApiResult.success(user);
    }

    @GetMapping("/logout")
    public ApiResult<Object> logOut() {
        return ApiResult.success(null, "注销成功");
    }
}
